#include "../Include/link_pub.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Student* create_student(const char* id, const char* name) {
    Student* student = (Student*)malloc(sizeof(Student));
    if (!student) return NULL;
    
    strcpy(student->student_id, id);
    strcpy(student->name, name);
    student->course_count = 0;
    student->total_credits = 0;
    memset(student->selected_courses, 0, sizeof(student->selected_courses));
    student->next = NULL;
    
    return student;
}

Student* add_student(Student* head, Student* new_student) {
    if (!head) return new_student;
    
    Student* current = head;
    while (current->next) {
        current = current->next;
    }
    current->next = new_student;
    return head;
}

Student* delete_student(Student* head, Course* course_head, const char* student_id) {
    if (!head) return NULL;
    
    // 查找要删除的学生
    Student* to_delete = NULL;
    Student* prev = NULL;
    Student* current = head;
    
    // 查找学生
    while (current && strcmp(current->student_id, student_id) != 0) {
        prev = current;
        current = current->next;
    }
    
    if (!current) return head; // 学生不存在
    
    to_delete = current;
    
    // 从其选修的课程中移除该学生，并减少课程人数
    for (int i = 0; i < to_delete->course_count; i++) {
        // 在课程链表中查找对应的课程
        Course* course = find_course(course_head, to_delete->selected_courses[i]);
        if (course) {
            // 从课程的选课列表中移除该学生
            for (int j = 0; j < course->enrolled_count; j++) {
                if (strcmp(course->enrolled_students[j], student_id) == 0) {
                    // 移动后续学生
                    for (int k = j; k < course->enrolled_count - 1; k++) {
                        strcpy(course->enrolled_students[k], course->enrolled_students[k+1]);
                    }
                    course->enrolled_count--;
                    break;
                }
            }
        }
    }
    
    // 从链表中删除学生
    if (prev) {
        prev->next = to_delete->next;
    } else {
        head = to_delete->next; // 删除头节点
    }
    
    free(to_delete);
    char log_msg[256];
    sprintf(log_msg, "Student %s deleted", student_id);
    log_operation("DELETE_STUDENT", log_msg);
    
    return head;
}

Student* modify_student(Student* head, const char* student_id, const char* new_name) {
    Student* student = find_student(head, student_id);
    if (student) {
        strcpy(student->name, new_name);
        char log_msg[256];
        sprintf(log_msg, "Student %s name changed to %s", student_id, new_name);
        log_operation("MODIFY_STUDENT", log_msg);
    }
    return head;
}

Student* find_student(Student* head, const char* student_id) {
    Student* current = head;
    while (current) {
        if (strcmp(current->student_id, student_id) == 0) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

void print_student(Student* student) {
    if (!student) {
        printf("Student not found\n");
        return;
    }
    
    printf("Student ID: %s\n", student->student_id);
    printf("Name: %s\n", student->name);
    printf("Total Credits: %d\n", student->total_credits);
    printf("Enrolled Courses (%d):\n", student->course_count);
    
    for (int i = 0; i < student->course_count; i++) {
        printf("  - %s\n", student->selected_courses[i]);
    }
}

void print_all_students(Student* head) {
    Student* current = head;
    printf("\n=== All Students ===\n");
    while (current) {
        print_student(current);
        printf("-------------------\n");
        current = current->next;
    }
}

int select_course(Student* student, Course* course) {
    if (!student || !course) return -1;
    
    // 检查是否已选
    for (int i = 0; i < student->course_count; i++) {
        if (strcmp(student->selected_courses[i], course->course_id) == 0) {
            return -2;
        }
    }
    
    // 检查课程容量
    if (course->enrolled_count >= course->max_capacity) {
        return -3;
    }
    
    if (student->course_count < 100) {
        strcpy(student->selected_courses[student->course_count], course->course_id);
        student->course_count++;
        student->total_credits += course->credits;
        
        // 同时更新课程的选课人数
        if (course->enrolled_count < 1000) {
            strcpy(course->enrolled_students[course->enrolled_count], student->student_id);
            course->enrolled_count++;
        }
        
        char log_msg[256];
        sprintf(log_msg, "Student %s enrolled in course %s", 
                student->student_id, course->course_id);
        log_operation("ENROLL_COURSE", log_msg);
        
        return 0;
    }
    
    return -1;
}

int drop_course(Student* student, Course* course) {
    if (!student || !course) return -1;
    
    int found = -1;
    for (int i = 0; i < student->course_count; i++) {
        if (strcmp(student->selected_courses[i], course->course_id) == 0) {
            found = i;
            break;
        }
    }
    
    if (found == -1) return -2;
    
    for (int i = found; i < student->course_count - 1; i++) {
        strcpy(student->selected_courses[i], student->selected_courses[i+1]);
    }
    student->course_count--;
    student->total_credits -= course->credits;
    
    // 从课程中移除学生
    found = -1;
    for (int i = 0; i < course->enrolled_count; i++) {
        if (strcmp(course->enrolled_students[i], student->student_id) == 0) {
            found = i;
            break;
        }
    }
    
    if (found != -1) {
        for (int i = found; i < course->enrolled_count - 1; i++) {
            strcpy(course->enrolled_students[i], course->enrolled_students[i+1]);
        }
        course->enrolled_count--;
    }
    
    char log_msg[256];
    sprintf(log_msg, "Student %s dropped course %s", 
            student->student_id, course->course_id);
    log_operation("DROP_COURSE", log_msg);
    
    return 0;
}